let FontPack = require('oled-font-pack')
let font = FontPack.oled_5x7

/*
列出所有可用字体
// console.log(FontPack._available);
字体库效果展示
http://www.rinkydinkelectronics.com/r_fonts.php
[
  'oled_3x5',
  'tiny_4x6',
  'oled_5x7',
  'small_6x8',
  'sinclair_8x8',
  'sinclair_inverted_8x8',
  'tiny_8x8',
  'cp437_8x8',
  'myke2_8x9',
  'small_8x12',
  'tron_8x12',
  'retro_8x16',
  'medium_numbers_12x16',
  'big_numbers_14x24',
  'arial_bold_16x16',
  'arial_italic_16x16',
  'arial_normal_16x16',
  'big_16x16',
  'franklin_gothic_normal_16x16',
  'hallfetica_normal_16x16',
  'nadianne_16x16',
  'sinclair_medium_16x16',
  'sinclair_medium_inverted_16x16',
  'swiss_721_outline_16x16',
  'various_symbols_16x16',
  'dot_matrix_medium_16x22',
  'dot_matrix_medium_zero_slash_16x22',
  'dot_matrix_medium_numbers_only_16x22',
  'arial_round_16x24',
  'ocr_a_extended_medium_16x24',
  'sixteen_segment_16x24',
  'grotesk_16x32',
  'grotesk_bold_16x32',
  'retro_16x32',
  'various_symbols_16x32',
  'various_symbols_v2_16x32',
  'dot_matrix_large_numbers_only_24x29',
  'inconsola_24x32',
  'ubuntu_24x32',
  'ubuntu_bold_24x32',
  'dingbats1_extra_large_32x24',
  'various_symbols_32x32'
]
*/

export default (oled:any, config:any) => {
  const { cpu, memory, disk, temp, ip } = config

  // 打开显示器
  // oled.turnOnDisplay()
  // 反向显示
  let show = false
  oled.invertDisplay(show)
  // 清理缓冲区
  oled.clearDisplay()
  // 画线
  // oled.drawLine(1, 1, 128, 32, 1);
  // 画矩形填充
  // oled.fillRect(1, 1, 10, 20, 1)
  /*
  obj font - JSON 格式的字体对象（请参阅下面关于获取字体的注释）
  int size - 字体大小，作为乘数。例如。2 将增加一倍，3 将增加三倍等等。
  字符串文本- 要在显示器上显示的实际文本。
  int color - 文本的颜色。可以指定为 0 表示“关闭”或黑色，指定为 1 或 255 表示“开启”或白色。
  bool wrapping - true 在屏幕限制处应用自动换行，false 表示不换行。如果提供一个没有空格的长字符串作为文本，则只应用字母换行
  */
  let CPU = `  ${cpu}`
  let DISK = ` ${disk.percent}`
  if (cpu >= 10) { CPU = ` ${cpu}` }
  if (cpu === 100) { CPU = ` 99` }
  if (disk.percent > 10) { DISK = disk.percent }

  oled.setCursor(5, 0)
  oled.writeString(font, 3, CPU, 1, false);
  oled.setCursor(57, 0)
  oled.writeString(font, 3, '%', 1, false);
  oled.setCursor(80, 4)
  oled.writeString(font, 1, `RAM: ${memory.percent}%`, 1, false);
  oled.setCursor(80, 14)
  oled.writeString(font, 1, `DISK:${DISK}%`, 1, false);
  oled.setCursor(20, 25)
  oled.writeString(font, 1, `IP:${ip}`, 1, false);
  oled.setCursor(0, 0)
  oled.writeString(font, 1, `${temp}°`, 1, false);
  // 右下角CPU使用率方块(x,y,w,h,true)  目前位置高度剩余22
  oled.fillRect(0, 10, 7, 1, 1);
  oled.fillRect(0, 10, 1, 22, 1);
  oled.fillRect(9, 10, 7, 1, 1);
  oled.fillRect(9, 10, 1, 22, 1);
  oled.fillRect(2, 12, 5, Math.ceil(Math.abs(temp - 40) * 1), 1);
  oled.fillRect(11, 12, 5, Math.ceil(cpu / 2), 1);
  oled.update()
}